export default {
	data() {
		return {
			loadStatus:"more", //more（loading前）、loading（loading中）、noMore（没有更多了）
			page:1,
			isLoadMore:false,  //是否加载中
			uid:null,
			where:"",
			data:[],
			// 微信收款参数
			options:{
				mchId: "",
				appId: "",
				package: "",
				openId: "",
			},
			// 订单号
			outTradeNo:'',
			list:[
				{
					"time":1605181066,
					"num":0.30,
					"num1":0.30,
				},
				{
					"time":1605181066,
					"num":0.30,
					"num1":0.30,
				},
				{
					"time":1605181066,
					"num":0.30,
					"num1":0.30,
				},
				{
					"time":1605181066,
					"num":0.30,
					"num1":0.30,
				},
				{
					"time":1605181066,
					"num":0.30,
					"num1":0.30,
				},
				{
					"time":1605181066,
					"num":0.30,
					"num1":0.30,
				},
			],
		}
	},
	methods: {
		// 用户确认收款，开始转账
		async handlePayment(item){
			console.log('item: ',item);
			// 微信转账
			uniCloud.callFunction({
				name: 'minectrl',
				data: {
					action: "mine/wxTransfer",
					data: {
						uid: this.uid,
						uniIdToken: this.token,
						outTradeNo:item.outTradeNo
					}
				}
			}).then((res) => {
				console.log("res:-----handlePayment ",res);
				if(res.result.errcode === 0 && res.result.wxState === 'WAIT_USER_CONFIRM'){
					this.options = res.result.options
					this.outTradeNo = res.result.outTradeNo
					this.wxReceivePayment(item._id,res.result.status)
				}else{
					// 提示错误信息
					uni.showToast({
						title: res.result.errmsg || '转账失败',
						icon: 'none'
					});
				}
			}).catch((err) => {
				console.log('err',err)
			})
		},
		// 用户微信收款
		wxReceivePayment(id,status){
			uni.showModal({
				title: '你有待确认的收款',
				content: '请及时收款，24小时内不领取将过期关闭！',
				confirmText:'去收款',
				showCancel:false, // 不显示取消按钮
				success: (res) => {
					if (res.confirm) {
						uni.requestMerchantTransfer({
							...this.options, // 变量 options 就是 上面接口的返回值中的 options 参数
							success: (res) => { 
								console.log('res: ',res);
								// {"errMsg": "确认收款"}
							},
							fail: (err) => {
								console.log('err: ',err);
								// 确认收款失败
							},
							complete: () => {
								// 通过 _id 找到对应的记录并更新
								const index = this.data.findIndex(i => i._id === id)
								if (index !== -1) {
									this.data[index].status = status
									this.getmsg(status)
								}
							}
						});
					} else if (res.cancel) {
						console.log('用户取消收款');
					}
				}
			})
		},
		loadData(e){
			this.data = e
			uni.hideLoading()
		},
		error(e){
			console.log("err",e)
			uni.hideLoading()
		},
		getmsg(type){
			if(type == 0){
				return '提现审核中'
			}else if(type == 1){
				return '提现处理中'
			}else if(type == 2){
				return '提现拒绝'
			}else if(type == 3){
				return '打款已完成'
			}else if(type == 4){
				return '打款失败'
			}
		}
	},
	filters: {
		timeStamp: function(value) {//具体到时分秒
			if (!value) return '';
			value = value * 1000
			var date = new Date(value); //时间戳为10位需*1000，时间戳为13位的话不需乘1000
			var year = date.getFullYear();
			var month = ("0" + (date.getMonth() + 1)).slice(-2);
			var sdate = ("0" + date.getDate()).slice(-2);
			var hour = ("0" + date.getHours()).slice(-2);
			var minute = ("0" + date.getMinutes()).slice(-2);
			var second = ("0" + date.getSeconds()).slice(-2);
			// 拼接
			var result = year + "-" + month + "-" + sdate + " " + hour + ":" + minute + ":" + second;
			// 返回
			return result;
		}
	},
	onLoad: function (options) {
		var self = this
		self.uid = uni.getStorageSync('uid');
		this.where = "uid=='"+this.uid+"'"
		uni.showLoading({
			title: '正在加载中...'
		})
	}
}